import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxConfig;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxParam;
import com.yoke.util.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by jiangzeyin on 2017/5/10.
 */
public class Listss {
    public static void main(String[] a) throws IOException {
        File file = new File("/etc/nginx/conf.d/ad/");
        File[] files = getConf(file, true);
        for (File item : files) {
            if (item.isDirectory()) {
                File[] childs = getConf(item, false);
                for (File item2 : childs)
                    scannerFile(item2);
            } else {
                scannerFile(item);
            }
        }
    }

    private static void scannerFile(File file) throws IOException {
        NgxConfig conf = NgxConfig.read(file.getPath());

        List<NgxEntry> locations = conf.findAll(NgxConfig.BLOCK, "server", "location");
        NgxParam server_name = conf.findParam("server", "server_name");
        List<String> stringList = server_name.getValues();
        if (stringList != null) {
            JSONArray jsonArray = new JSONArray();
            for (String rootUrl : stringList) {
                JSONArray jsonArray_ = scannerUrl(rootUrl, locations, null);
                if (jsonArray_ != null)
                    jsonArray.addAll(jsonArray_);
            }
        }
    }

    private static JSONArray scannerUrl(String rootUrl, List<NgxEntry> locations, JSONArray jsonArray) {
        if (locations == null)
            return null;
        if (jsonArray == null)
            jsonArray = new JSONArray();
        for (NgxEntry ngxEntry : locations) {
            if (ngxEntry instanceof NgxBlock) {
                NgxBlock block = (NgxBlock) ngxEntry;
                NgxParam root = block.findParam("root");
                if (root != null) {
                    List<String> rootValues = block.getValues();
                    if (rootValues.size() == 1) {
                        //System.out.println(root.getValue());
                        JSONArray jsonArray_ = scannerDir(rootUrl, root.getValue(), root.getValue(), null);
                    }
                }
                NgxParam rewrite = block.findParam("rewrite");
                if (rewrite != null) {
                    List<String> values = rewrite.getValues();
                    if (values.size() == 3) {
                        String url = values.get(1);
                        if (url.endsWith("$2"))
                            url = url.substring(0, url.length() - 2);
                        String locUrl = rootUrl + block.getValue();
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("url", locUrl);
                        jsonObject.put("toUrl", url);
                        jsonObject.put("type", "rewrite");
                        jsonArray.add(jsonObject);
                        //System.out.println(locUrl + "  " + url);
                    }
                    //System.out.println(values.size());
                }
                NgxParam proxy_pass = block.findParam("proxy_pass");
                if (proxy_pass != null) {
                    String url = rootUrl + block.getValue();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("url", url);
                    jsonObject.put("type", "proxy");
                    jsonArray.add(jsonObject);
                }
            }
        }
        return jsonArray;
    }

    private static JSONArray scannerDir(String rootUrl, String root, String path, JSONArray array) {
        File file = new File(path);
        if (!file.exists())
            return null;
        if (array == null)
            array = new JSONArray();
        File[] files = file.listFiles((dir, name) -> {
            if (dir.isDirectory())
                return true;
            if (name.endsWith(".html"))
                return true;
            return false;
        });
        for (File item : files) {
            if (item.isDirectory()) {
                JSONArray jsonArray = scannerDir(rootUrl, root, item.getPath(), array);
                array.addAll(jsonArray);
                return array;
            } else {
                String name = item.getName();
                if (name.equals("index.html")) {
                    String url = FileUtil.ClearPath(rootUrl + "/" + FileUtil.ClearPath(item.getPath()).replace(root, ""));
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("type", "file");
                    jsonObject.put("url", url);
                    array.add(url);
                }
            }
        }
        return array;
    }

    private static File[] getConf(File file, boolean child) {
        File[] files = file.listFiles((dir, name) -> {
            if (name.endsWith(".conf"))
                return true;
            if (child && dir.isDirectory())
                return true;
            return false;
        });
        return files;
    }
}
